package com.xyz.eventoper;

import com.free.common.vo.ResultModel;
import com.free.exception.SystemException;
import com.free.utils.DateUtils;
import com.xyz.blockqueue.QueueModel;
import com.xyz.dao.logic.CCustomersignLogic;
import com.xyz.dao.logic.SSettingLogic;
import com.xyz.domain.constants.ConstantsSysSetting;
import com.xyz.domain.ext.EnumEventType;
import com.xyz.domain.ext.EnumFundFlowForm;
import com.xyz.domain.ext.EnumOperLogType;
import com.xyz.domain.table.CCustomer;
import com.xyz.domain.table.CCustomerfundflow;
import com.xyz.domain.table.CCustomersign;
import com.xyz.domain.table.SSetting;
import com.xyz.service.CustomerFundService;
import com.xyz.service.SSettingService;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public class SignEventOperator extends AbstractEventOper {

    @Autowired
    private CCustomersignLogic signLogic;

    @Autowired
    private SSettingService settingService;

    @Autowired
    private CustomerFundService fundService;

    @Override
    public EnumEventType getEventType() {
        return EnumEventType.SignEvent;
    }

    @Override
    public ResultModel operate(QueueModel model) {
        CCustomer customer = model.getCustomerInfo();
        try {
            CCustomersign sign =  signLogic.getTodaySignInfo(customer);
            if(sign == null){
                return ResultModel.failure();
            }
            Date current = new Date();
            Date day28Info =DateUtils.convert2Date(DateUtils.convert2String(current, DateUtils.FormatEnum.yyyyMM) + "-28", DateUtils.FormatEnum.yyyy_MM_dd);
            if(current.compareTo(day28Info) >= 0){
                int cnt = signLogic.getMonthSignCount(customer.getId(), current);
                if(cnt >= 28 && !isMonthSignGeted(customer.getId()).isSuccess()){
                   return rewardMonth(customer.getId());
                }
            }
            return rewardDay(customer.getId());
        } catch (Exception e) {
            return ResultModel.failure();
        }
    }

    private ResultModel rewardMonth(Integer customerId){
        SSetting settingInfo = getSettingInfo(ConstantsSysSetting.C_SIGN002);
        if(settingInfo == null){
            return ResultModel.failure();
        }else{
            return fundService.addFundInfo(EnumFundFlowForm.REWARD_MONTHSIGN, customerId, settingInfo.getAmtValue(), BigDecimal.ZERO, BigDecimal.ZERO);
        }
    }

    private ResultModel rewardDay(Integer customerId){
        ResultModel res = isDayGeted(customerId);
        if(!res.isSuccess()){
            SSetting settingInfo = getSettingInfo(ConstantsSysSetting.C_SIGN001);
            if(settingInfo == null){
                return ResultModel.failure();
            }
            BigDecimal lstVal = settingInfo.getAmtValue().multiply(new BigDecimal(Math.random())).setScale(0,BigDecimal.ROUND_DOWN);
            return fundService.addFundInfo(EnumFundFlowForm.REWARD_SIGN, customerId, lstVal, BigDecimal.ZERO, BigDecimal.ZERO);
        }
        return res;
    }

    public SSetting getSettingInfo(String code){
        List<SSetting> settings = settingService.getSettings(ConstantsSysSetting.T_SIGN);
        SSetting settingInfo = settings.stream().filter(x->{
           return x.getCode().equals(code);
        }).findFirst().orElse(null);
        return settingInfo;
    }

    private ResultModel isDayGeted(Integer customerId){
        Date current = new Date();
        String startTime = DateUtils.convert2String(DateUtils.getMonthFirstDay(current), DateUtils.FormatEnum.yyyy_MM_dd);
        CCustomerfundflow cCustomerfundflow = new CCustomerfundflow();
        cCustomerfundflow.setForm(EnumFundFlowForm.REWARD_SIGN.getKey());
        cCustomerfundflow.setCustomerId(customerId);
        cCustomerfundflow.setStartTime(startTime);
        List<CCustomerfundflow> resList = fundService.getRangeTypeInfo(cCustomerfundflow);
        if(resList == null || resList.size() == 0){
            return ResultModel.failure();
        }
        return ResultModel.success();
    }

    private ResultModel isMonthSignGeted(Integer customerId){
        Date current = new Date();
        String startTime = DateUtils.convert2String(DateUtils.getMonthFirstDay(current), DateUtils.FormatEnum.yyyy_MM_dd);
        String endTime = DateUtils.now(DateUtils.FormatEnum.yyyy_MM_dd_HH_mm_ss);
        CCustomerfundflow cCustomerfundflow = new CCustomerfundflow();
        cCustomerfundflow.setForm(EnumFundFlowForm.REWARD_MONTHSIGN.getKey());
        cCustomerfundflow.setCustomerId(customerId);
        cCustomerfundflow.setStartTime(startTime);
        cCustomerfundflow.setEndTime(endTime);
        List<CCustomerfundflow> resList = fundService.getRangeTypeInfo(cCustomerfundflow);
        if(resList == null || resList.size() == 0){
            return ResultModel.failure();
        }
        return ResultModel.success();
    }



    public static void main(String[] args) {
        Date current = new Date();
        current = DateUtils.getMonthLastDay(current);
        current = DateUtils.getMonthFirstDay(current);
        int j = 0;
    }



}
